package homegroup.inc.servlets;

import homegroup.inc.dao.BookDAO;
import homegroup.inc.dto.Book;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by stardust on 29.10.2014.
 */

@WebServlet(urlPatterns = "/index")
public class Start extends HttpServlet {

    private ArrayList<Book> bookList = new ArrayList<Book>();

    @EJB
    protected BookDAO bookDAO;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        createTheBook();

        for(Book book : bookList) {
            bookDAO.create(book);
        }
        List<Book> books = bookDAO.findByISBN("b");

        PrintWriter out = resp.getWriter();

        out.print("<h1>Books isbn");
        for(Book book : books) {
            out.println(book.getISBN());
        }
        out.print("</h1)");
    }

    private boolean createTheBook() {

        Book book1 = new Book();
        book1.setDescription("Book descr");
        book1.setAuthor("David");
        book1.setISBN("b1");
        book1.setPublisher("O'Raily");
        book1.setTitle("title of the book#1");
        book1.setBookPath("bookpath#1");
        book1.setCoverPath("coverpath#1");

        Book book2 = new Book();
        book2.setDescription("Book descr");
        book2.setAuthor("David");
        book2.setISBN("b2");
        book2.setPublisher("O'Raily");
        book2.setTitle("title of the book#2");
        book2.setBookPath("bookpath#2");
        book2.setCoverPath("coverpath#2");

        Book book3 = new Book();
        book3.setDescription("Book descr");
        book3.setAuthor("David");
        book3.setISBN("b3");
        book3.setPublisher("O'Raily");
        book3.setTitle("title of the book#3");
        book3.setBookPath("bookpath#3");
        book3.setCoverPath("coverpath#3");

        bookList.add(book1);
        bookList.add(book2);
        bookList.add(book3);
        return true;
    }
}
